package com.woniu.day8.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.woniu.day8.mapper.BuildingMapper;
import com.woniu.day8.pojo.BuildingPo;
import com.woniu.day8.service.BuildingServicer;
import org.springframework.stereotype.Service;

@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, BuildingPo> implements BuildingServicer {
    @Override
    public Page<BuildingPo> buildPageInfo(Page<BuildingPo> page, BuildingPo buildingpo){
        LambdaQueryWrapper<BuildingPo> queryWrapper = new LambdaQueryWrapper<>();
        // 添加各种条件查询（如果字段为空或无效则不会添加到查询条件中）
        if (buildingpo.getId() != null ) {
            queryWrapper.eq(BuildingPo::getId, buildingpo.getId());//id查询 使用eq进行精确匹配
        }
        if (buildingpo.getNumbers() != null && !buildingpo.getNumbers().isEmpty()) {
            queryWrapper.like(BuildingPo::getNumbers, buildingpo.getNumbers());//栋名
        }
        if (buildingpo.getUnits() != null && !buildingpo.getUnits().isEmpty()) {
            queryWrapper.like(BuildingPo::getUnits, buildingpo.getUnits());//单元名
        }
        if (buildingpo.getRemarks() != null && !buildingpo.getRemarks().isEmpty()) {
            queryWrapper.like(BuildingPo::getRemarks, buildingpo.getRemarks());//全名
        }
        // 按楼栋编号的数字部分排序
        // 使用 SUBSTRING_INDEX 提取数字部分
        queryWrapper.last("ORDER BY CAST(SUBSTRING_INDEX(numbers, '栋', 1) AS UNSIGNED) ASC");

        return this.page(page, queryWrapper);
    }


}
